package com.hangim.pce.common.service.impl;

import com.hangim.pce.common.domain.IdEntity;
import com.hangim.pce.common.persistence.mybatis.BaseEntityMapper;
import com.hangim.pce.common.service.BaseEntityService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @see BaseEntityService
 */
public abstract class BaseEntityServiceImpl<M extends BaseEntityMapper<T>, T extends IdEntity> extends BaseServiceImpl implements BaseEntityService<T> {
    @Autowired
    protected M mapper;

    /**
     * @see BaseEntityService#insert(T entity)
     */
    @Override
    public Integer insert(T entity) {
        // 生成ID
        entity.setId(generateNextId());
        return this.mapper.insert(entity);
    }

    /**
     * @see BaseEntityService#updateById(T entity)
     */
    @Override
    public Integer updateById(T entity) {
        return this.mapper.updateById(entity);
    }

    /**
     * @see BaseEntityService#save(T entity)
     */
    @Override
    public Integer save(T entity) {
        if (entity.getId() != null && entity.getId() > 0) {
            return updateById(entity);
        } else {
            return insert(entity);
        }
    }

    /**
     * @see BaseEntityService#selectById(Long id)
     */
    @Override
    public T selectById(Long id) {
        return this.mapper.selectById(id);
    }

    /**
     * @see BaseEntityService#deleteById(Long id)
     */
    @Override
    public Integer deleteById(Long id) {
        return this.mapper.deleteById(id);
    }

    /**
     * @see BaseEntityService#deleteBatchIds(List ids)
     */
    @Override
    public Integer deleteBatchIds(List<Long> ids) {
        return this.mapper.deleteBatchIds(ids);
    }

    /**
     * @see BaseEntityService#delete(T entity)
     */
    @Override
    public Integer delete(T entity) {
        return this.mapper.delete(new EntityWrapper<>(entity));
    }

    /**
     * @see BaseEntityService#selectAll(T entity)
     */
    @Override
    public List<T> selectAll(T entity) {
        return this.mapper.selectList(new EntityWrapper<>(entity));
    }

    /**
     * @see BaseEntityService#selectPage(Page page)
     */
    @Override
    public Page<T> selectPage(Page<T> page) {
        page.setRecords(this.mapper.selectPage(page, null));
        return page;
    }

    /**
     * @see BaseEntityService#selectPage(Page page)
     */
    @Override
    public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
        page.setRecords(this.mapper.selectPage(page, wrapper));
        return page;
    }

    /**
     * @see BaseEntityService#selectBatchIds(List idList)
     */
    @Override
    public List<T> selectBatchIds(List<Long> idList) {
        return this.mapper.selectBatchIds(idList);
    }

    /**
     * @see BaseEntityService#selectOne(T condition)
     */
    @Override
    public T selectOne(T condition) {
        List<T> list = this.mapper.selectList(new EntityWrapper<>(condition));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
